package com.example.wishstar.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment;

import com.example.wishstar.R;
import com.example.wishstar.model.Habit;
import com.example.wishstar.model.UpdateHabit;
import com.example.wishstar.utils.ScreenUtil;

public class UpdateHabitDialogFragment extends DialogFragment {

    private String TAG = "UpdateHabitDialogFragment";
    private ImageView updateHabitImage;
    private Bundle bundle = null;
    private TextView updateHabitTextTitle, updateImageBackground;
    private EditText updateHabitEditTextDetails;
    private Button buttonCheck, buttonFile;
    private ImageView imageClose;

    private Habit habit;
    private Context mcontext;
    private UpdateHabitCallBack updateHabitCallBack;

    public interface UpdateHabitCallBack{
        void updateHabit(UpdateHabit habit);
    }

    public void setUpdateHabitCallBack(UpdateHabitCallBack updateHabitCallBack) {
        this.updateHabitCallBack = updateHabitCallBack;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialogfragment_update_habit, container, false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//1.必须设置dialog的window背景为透明颜色，不然圆角无效或者是系统默认的颜色

        updateHabitImage = view.findViewById(R.id.update_habit_image);
        updateHabitTextTitle = view.findViewById(R.id.update_habit_title_text);
        updateHabitEditTextDetails = view.findViewById(R.id.update_habit_edittext);
        buttonCheck = view.findViewById(R.id.update_habit_button_check);
        buttonFile = view.findViewById(R.id.update_habit_button_file);
        updateImageBackground = view.findViewById(R.id.update_habit_image_bg);
        imageClose = view.findViewById(R.id.update_habit_image_close);

        bundle = getArguments();
        mcontext = getContext();

        if (bundle != null) {
            habit = new Habit();
            habit.habitId = bundle.getInt("habitId");
            habit.habitTitle = bundle.getString("habitTitle");
            habit.habitIcon = bundle.getInt("habitIcon");
            Log.e(TAG, habit.toString());

            GradientDrawable gradientDrawable = (GradientDrawable) updateImageBackground.getBackground();
            gradientDrawable.setColor(mcontext.getColor(R.color.white));
            updateHabitTextTitle.setText(habit.habitTitle);
            switch (habit.habitIcon) {
                case 1:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_water));
                    break;
                case 2:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_agriculture));
                    break;
                case 3:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_apple));
                    break;
                case 4:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_book));
                    break;
                case 5:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_drink));
                    break;
                case 6:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_economy));
                    break;
                case 7:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_egg));
                    break;
                case 8:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_listen));
                    break;
                case 9:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_mask));
                    break;
                case 10:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_pet));
                    break;
                case 11:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_run));
                    break;
                case 12:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_shopping));
                    break;
                case 13:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_swimming));
                    break;
                case 14:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_teeth));
                    break;
                case 15:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_sun));
                    break;
                case 16:
                    updateHabitImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.habit_item_icon_agenda));
                    break;
            }
        }

        buttonCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                habit.habitDetails = updateHabitEditTextDetails.getText().toString();
                UpdateHabit updateHabit = new UpdateHabit(habit.habitId, habit.habitDetails, 1);
                updateHabitCallBack.updateHabit(updateHabit);
                dismiss();
            }
        });

        buttonFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                habit.habitDetails = updateHabitEditTextDetails.getText().toString();
                UpdateHabit updateHabit = new UpdateHabit(habit.habitId, habit.habitDetails, 2);
                updateHabitCallBack.updateHabit(updateHabit);
                dismiss();
            }
        });

        imageClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        return view;
    }

    /**
     * 修改布局的大小
     */
    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
        resizeDialogFragment();
    }

    private void resizeDialogFragment() {
        Dialog dialog = getDialog();
        if (null != dialog) {
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
            lp.height = (ScreenUtil.getScreenHeight(getContext()) / 2);//获取屏幕的宽度，定义自己的宽度
            lp.width = (9 * ScreenUtil.getScreenWidth(getContext()) / 10);
            if (window != null) {
                window.setLayout(lp.width, lp.height);
            }
        }
    }
}
